package com.yqfk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqfk.auto.JSONAuthentication;
import com.yqfk.entity.YPun;
import com.yqfk.entity.YVac;
import com.yqfk.mapper.YPunMapper;
import com.yqfk.mapper.YUserMapper;
import com.yqfk.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yqfk
 * @since 2022-03-30
 */
@RestController
@RequestMapping("/y-pun")
public class YPunController extends JSONAuthentication {
    @Autowired
    YPunMapper yPunMapper;
    @Autowired
    YUserMapper yUserMapper;
    @ApiOperation(value = "添加用户打卡信息")
    @PostMapping("/add")
    public void addCar(HttpServletRequest request, HttpServletResponse response, @RequestBody YPun yPun) throws Exception {
        Date date=new Date();
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date);
        int year1=calendar.get(Calendar.YEAR);
        int day1=calendar.get(Calendar.DAY_OF_YEAR);

        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",yPun.getUserId());
        List<YPun> list = yPunMapper.selectList(wrapper);

        boolean flag=true;
        for (YPun a:list){
            Date date1=a.getTime();
            Calendar calendar1=Calendar.getInstance();
            calendar1.setTime(date1);
            int year2=calendar1.get(Calendar.YEAR);
            int day2=calendar1.get(Calendar.DAY_OF_YEAR);
            if((year1==year2)&&(day1==day2)){
                flag=false;
                break;
            }else{
                flag=true;
            }
        }
       if (flag){
           Timestamp timestamp =new Timestamp(date.getTime());
           yPun.setTime(timestamp);
           if (yPunMapper.insert(yPun) > 0) {
               Result ok = Result.ok().message("打卡成功");
               this.WriteJSON(request, response, ok);
           }else{
               Result error = Result.error().message("打卡失败");
               this.WriteJSON(request, response, error);
           }
       }else{
           Result error = Result.error().message("一天只能打卡一次");
           this.WriteJSON(request, response, error);
       }
    }
    @ApiOperation(value = "删除用户打卡")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = yPunMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改用户打卡信息")
    @PostMapping("/upd")
    public void upd(HttpServletRequest request, HttpServletResponse response, @RequestBody YPun yPun) throws Exception {
        if (yPunMapper.updateById(yPun) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询全部用户打卡信息")
    @PostMapping("/findAll")
    public void findAll(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        Page<YPun> shUserPage = new Page<>(current, size);
        Page<YPun> shUserPages = yPunMapper.selectPage(shUserPage, null);
        List<YPun> records = shUserPages.getRecords();
        int total = yPunMapper.selectCount(null);
        for (YPun a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("ym", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户id查询用户打卡信息")
    @PostMapping("/findDaKaByUserId")
    public void findDaKaByUserId(HttpServletRequest request, HttpServletResponse response,@RequestParam("userId") int userId, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId);
        Page<YPun> shUserPage = new Page<>(current, size);
        Page<YPun> shUserPages = yPunMapper.selectPage(shUserPage, wrapper);
        List<YPun> records = shUserPages.getRecords();
        int total = yPunMapper.selectCount(wrapper);
        for (YPun a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("ym", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询健康状态不为健康的用户打卡记录")
    @PostMapping("/findDaKaByUserIdNoJK")
    public void findDaKaByUserIdNoJK(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.ne("per_sta","健康");
        Page<YPun> shUserPage = new Page<>(current, size);
        Page<YPun> shUserPages = yPunMapper.selectPage(shUserPage, wrapper);
        List<YPun> records = shUserPages.getRecords();
        int total = yPunMapper.selectCount(wrapper);
        for (YPun a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("ym", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查询家人有发烧的打卡记录")
    @PostMapping("/findDaKaByUserIdJRFS")
    public void findDaKaByUserIdJRFS(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.ne("fam_sta","有");
        Page<YPun> shUserPage = new Page<>(current, size);
        Page<YPun> shUserPages = yPunMapper.selectPage(shUserPage, wrapper);
        List<YPun> records = shUserPages.getRecords();
        int total = yPunMapper.selectCount(wrapper);
        for (YPun a:records){
            a.setYUser(yUserMapper.selectById(a.getUserId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("ym", records).data("total", total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
}

